var subtotalprice = document.getElementById('subtotalprice');
$(document).ready(function () {
    $('.quantity').on('input', function () {
        var quantity = $(this).val();
        var item = $(this); //取得变化量

        var price = document.getElementById('price' + item.attr('name')); //计算单价
        var pricestr = price.textContent.trim();
        var priceStr = '';
        for(var i = 1; i < pricestr.length; i++){
            priceStr += pricestr.charAt(i);
        }
        var totalprice = document.getElementById('totalprice' + item.attr('name')); //初始总价格
        var totalpricestr = totalprice.textContent.trim();
        var totalpriceStr = '';
        for(var i = 1; i < totalpricestr.length; i++){
            totalpriceStr += totalpricestr.charAt(i);
        }
        var subtotalpricestr = subtotalprice.textContent.trim(); //原总价格
        var subtotalpriceStr = '';
        for(var i = 12; i < subtotalpricestr.length; i++){
            subtotalpriceStr += subtotalpricestr.charAt(i);
        }

        subtotalpriceStr -= totalpriceStr; //计算新的总价格
        totalpriceStr = quantity.toString() * priceStr.toString();
        subtotalpriceStr += totalpriceStr;

        $.ajax({
            data: {
                quantity: quantity,
                itemId: item.attr('name')
            },
            url: 'http://localhost:8080/mypetstore/updateAJAX',
            type: 'POST',

            success: function (res) {
                totalpriceStr = '$' + totalpriceStr;
                totalprice.textContent = totalpriceStr
                subtotalpriceStr = 'Sub Total: $' + subtotalpriceStr;
                subtotalprice.textContent = subtotalpriceStr;

                var orderQuantity = document.getElementById('orderQuantity' + item.attr('name'));
                orderQuantity.textContent = quantity;
                var orderTotal = document.getElementById('orderTotal' + item.attr('name'));
                orderTotal.textContent = totalpriceStr;
            },
            error: function (res){
            }
        });
    });
    
    $('.Button').on('click', function (event) {
        event.preventDefault();
        var button = $(this);
        var btn = document.getElementById(button.attr('id'));
        var userConfirmed = confirm('Are you sure you want to remove this item?');

        if (userConfirmed) {
            $.ajax({
                data: {
                    workingItemId: button.attr('id')
                },
                url: 'http://localhost:8080/mypetstore/removeAJAX',
                type: 'POST',

                success: function (res) {
                    var curSubtotalpricestr = subtotalprice.textContent.trim(); //原总价格
                    var curSubtotalpriceStr = '';
                    for(var i = 12; i < curSubtotalpricestr.length; i++){
                        curSubtotalpriceStr += curSubtotalpricestr.charAt(i);
                    }
                    var curTotalprice = document.getElementById('totalprice' + button.attr('id'));
                    var curTotalpricestr = curTotalprice.textContent.trim();
                    var curTotalpriceStr = '';
                    for(var i = 1; i < curTotalpricestr.length; i++){
                        curTotalpriceStr += curTotalpricestr.charAt(i);
                    }
                    curSubtotalpriceStr -= curTotalpriceStr;
                    curSubtotalpriceStr = 'Sub Total: $' + curSubtotalpriceStr;
                    subtotalprice.textContent = curSubtotalpriceStr;

                    $('#cartItem' + button.attr('id')).remove();
                },
                error: function (res){
                }
            });
        }
    });
});
